/*
 * @Author: 詹真琦(legendryztachi@gmail.com)
 * @Date: 2022-01-19 10:44:13
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @LastEditTime: 2022-04-28 17:18:04
 * @FilePath: \code\cwp-front\pc-collaboration\src\handles\handle-list\summary\modules\cancel.js
 * @Description: 撤销
 */
import { chooseAttitude } from './return/helper';
import {useBaseInfoWithOut} from "@/store/modules/base-info";
import globalMethods from "@/plugins/methods";
import globalData from '@/plugins/data';
import {fireEvent, getCollEventParams} from "@/handles/common/sdk";
import {useBaseInfoStore} from "@/store/modules/base-info";

/**
 * 检查能否撤销
 * @returns {boolean}
 */
function checkCanRepeal(){
  const useBaseInfo = useBaseInfoWithOut();
  const {
    summary:{
      id:summaryId,
      processId,
    },
    affair:{activityId}
  }=useBaseInfo.businessData;
  let params = new Object();
  params[ "summaryId" ] = summaryId;
  //校验是否流程结束、是否审核、是否核定，涉及到的子流程调用工作流接口校验
  let canDealCancel = window.callBackendMethod("colManager", "checkIsCanRepeal", params);
  if (canDealCancel.msg != null) {
    globalMethods.warning({
      content: globalMethods.i18n(canDealCancel.msg),
    });
    return false;
  }
  //添加流程锁
  var result = window.onBeforeWorkflowOperationValidate(processId, "", activityId, "", "", "collaboration", "repeal");
  if (result && !result.pass) {
    globalMethods.warning({
      content: globalMethods.i18n(result.msg),
    });
    return false;
  }
  return true;
}

/**
 * 释放锁回调
 */
function releaseLock() {
  const useBaseInfo = useBaseInfoWithOut();
  const {
    summary: {
      processId,
    },
    currentUser: {id: userId}
  } = useBaseInfo.businessData;
  window.releaseWorkflowByAction(processId, userId, 12);
  //去掉遮罩
  useBaseInfo.setIsHideGlobalLoading();
}
export default function (
  btn = {
    label: $.i18n('collaboration.nodePerm.repeal.label'),
    handle: 'cancel',
    handleOriginal: 'Cancel'
  }
) {
  return new Promise(async (resolve) => {
    if (!checkCanRepeal()) {
      return resolve();
    }
    const businessData = useBaseInfoStore().baseInfo?.businessData || {};
    const eventParams = getCollEventParams(businessData, globalData);
    let eventResult = await fireEvent("Cancel", eventParams);
    if (!eventResult) {
      return resolve();
    }
    chooseAttitude.call(this, btn, null, releaseLock);
    return resolve();
  });
}
